<!doctype html>
<html>
<head>
    <title>订单添加</title>
    <?php $this->load->module('admin/index/page_header');?>
</head>
<body>
<div class="wrap">
    <div id="home_toptip"></div>
    <div class="nav">
        <ul>
            <li><a href="<?php echo $index_url;?>">酒店订单</a></li>
            <li class="current"><a href="javascript:;">添加订单</a></li>
        </ul>
    </div>
    <div class="table_full">


        <div class="nav">
            <ul class="cc tab" id="js-config-menu">
                <li data-type="base_conf" class="current"><a href="javascript:void(0);">基本信息</a></li>
                <li data-type="room_conf" ><a href="javascript:void(0);">安排房间</a></li>
                <li data-type="goods_conf" ><a href="javascript:void(0);">菜品管理</a></li>
                <li data-type="drinks_conf" ><a href="javascript:void(0);">酒水管理</a></li>
            </ul>
        </div>



        <form class="formvalidate" action="<?php echo $form_url;?>" method="post">

            <!-- 基本信息-->

            <div id="base_config" class="js-config-warp">


                <div class="h_a">价格信息</div>
                <div class="table_full">
                    <table class="tab_label" width="100%">
                        <col style="width: 150px;"/>
                        <col/>
                        <tbody>
                        <tr>
                            <th>单价</th>
                            <td>¥ <?php echo $book_unit_price; ?> / 人 / 天</td>
                        </tr>
                        <tr>
                            <th>天数</th>
                            <td><span class="js-days"><?php echo $days?></span> 天</td>
                        </tr>
                        <tr>
                            <th>人数</th>
                            <td><span class="js-numbers">6</span> 人</td>
                        </tr>

                        </tbody>
                    </table>
                </div>

                <div class="h_a">入住信息</div>
                <div class="table_full">
                    <table class="tab_label" width="100%">
                        <col style="width: 150px;"/>
                        <col/>
                        <tbody>


                        <tr>
                            <th>入住日期</th>
                            <td><input type="text" class="input rounded" value="<?php echo  $start;?>" placeholder="请选择入住日期" name="start_date" id="start_date" readonly /></td>
                        </tr>


                        <tr>
                            <th>离店日期</th>
                            <td><input type="text" class="input rounded" data-default="<?php echo $end;?>" placeholder="请选择离店日期"  value="<?php echo  $end;?>" name="end_date" id="end_date" readonly /></td>
                        </tr>

                        <tr>
                            <th>成人男数量</th>
                            <td><input type="text" class="input rounded js-input-numbers"  name="male_number" id="male_number" value="3" placeholder="请输入成人男人数" maxlength="2" validate="{required:true,number:true}"  /></td>
                        </tr>
                        <tr>
                            <th>成人女数量</th>
                            <td><input type="text" class="input rounded js-input-numbers"  name="woman_number" id="woman_number" value="3" placeholder="请输入成人女人数"  maxlength="2" validate="{required:true,number:true}"  /></td>
                        </tr>

                        <tr>
                            <th>儿童数量</th>
                            <td><input type="text" class="input rounded js-input-numbers"  name="child_number" id="child_number" value="0" placeholder="请输入儿童人数"  maxlength="2" validate="{required:true,number:true}"  /></td>
                        </tr>
                        
                        </tbody>
                    </table>
                </div>

                <div class="h_a">联系信息</div>
                <div class="table_full">
                    <table class="tab_label" width="100%">
                        <col style="width: 150px;"/>
                        <col/>
                        <tbody>
                        <tr>
                            <th>微信用户</th>
                            <td><input type="hidden"   name="wx_nick" id="js-wx-nick" />
                                <select name="user_id" class="select_5" id="js-user-id">
                                    <option value="0">暂不选择</option>
                                    <?php if($member_list):?>
                                        <?php foreach ($member_list as $key=>$val):?>
                                            <option value="<?php echo $val['user_id']?>"><?php echo $val['username']?></option>
                                        <?php endforeach;?>
                                    <?php endif;?>
                                </select>
                            </td>

                        </tr>
                        <tr>
                            <th>姓名</th>
                            <td><input type="text" class="input rounded" name="contact_name" id="js-contact-name" placeholder="请输入姓名" validate="{required:true}" /></td>
                        </tr>
                        <tr>
                            <th>手机号</th>
                            <td><input type="text" class="input rounded" name="contact_phone_num"  id="js-contact-phone-num" placeholder="请输入手机号" validate="{required:true,isMobile:true,}" /></td>
                        </tr>

                        </tbody>
                    </table>
                </div>
                <div class="h_a">订单备注</div>
                <div class="table_full">
                    <textarea name="order_note" style="width: 600px;"></textarea>
                </div>

            </div>




            <!-- 安排房间-->
            <div id="room_config" class="js-config-warp" style="display: none" >

                <div id="price_id">
                <div class="h_a">
                    入住房间
                    &nbsp; &nbsp; &nbsp; <span class="red">(
                    <span class="red js-span-child">儿童数: 0 人</span> &nbsp;
                    <span class="red js-span-male">成人男数: 0 人</span> &nbsp;
                    <span class="red js-span-woman">成人女数: 0 人</span>
                        )</span>
                </div>
                <div class="table_full">
                    <table class="table_list" width="100%" id="js-input-check-box">
                        <col width="100px"/>
                        <col width="200px"/>
                        <col width="200px"/>
                        <col width="200px"/>
                        <col width="200px"/>
                        <col width="200px"/>
                        <col width="200px"/>
                        <col/>
                        <tbody>
                        <tr>
                            <td>选择</td>
                            <td>房间号</td>
                            <td>已经入住日期</td>
                            <td>价格</td>
                            <td>入住人数</td>
                            <td>建议人数</td>
                            <td>备注</td>
                        </tr>

                        <?php if($room_list):?>
                            <?php foreach ($room_list as $key=>$item):?>
                                <tr class="js-input-room-check">
                                    <td> <input type="checkbox" name="room_id[]" value="<?php echo $item['id'];?>"
                                               <?php if (in_array($item['id'],$lock_room)):?>checked  disabled data-status="0" <?php endif;?>
                                        ><?php if (in_array($item['id'],$lock_room)):?>已入住<?php endif;?> </td>
                                    <td><?php echo $item['name'];?></td>
                                    <td>
                                        <?php if(!empty($lock_key_date[$item['id']])):?>
                                            <?php echo implode(',', $lock_key_date[$item['id']]);?>
                                            <?php else: ?>
                                            未入住
                                        <?php endif;?>
                                    </td>

                                    <td>￥<?php echo $item['price'];?></td>

                                    <td><i class="fa fa-minus-square js-market-minus user-icon"></i> <input type="text" data-price="<?php echo $item['price'];?>" style="width: 20px;margin: 0 3px;" name="pricemarket[]" class="input rounded js-item-good-input" value="0"  <?php if (in_array($item['id'],$lock_room)):?>  disabled <?php endif;?> ><i class="fa fa-plus-square js-market-plus user-icon"></i> </td>
                                    <td><?php echo $item['number'];?></td>
                                    <td><?php echo $item['note'];?></td>

                                </tr>
                            <?php endforeach;?>
                        <?php endif;?>

                        </tbody>
                    </table>

                </div>
            </div>

            </div>

            <!-- 菜品管理-->
            <div id="goods_config" class="js-config-warp" style="display: none" >

                        <div class="h_a">
                            菜品管理
                </div>
                <div class="table_full">
                    <table class="table_list" width="100%">

                        <col width="200px"/>
                        <col width="200px"/>
                        <col width="200px"/>
                        <col width="200px"/>
                        <col width="200px"/>

                        <col/>
                        <tbody class="goods_body">
                        <tr>

                            <td>ID</td>
                            <td>菜品名</td>
                            <td>价格</td>
                            <td>备注</td>
                            <td>已选数量</td>
                        </tr>

                        <?php if($goods_list):?>
                            <?php foreach ($goods_list as $key=>$item):?>
                                <tr>
                                    <input type="hidden" name="gids[]" value="<?php echo $item['id'];?>">
                                    <td><?php echo $item['id'];?></td>
                                    <td><?php echo $item['name'];?></td>
                                    <td><?php echo price_format($item['money']);?></td>
                                    <td><?php echo $item['note'];?></td>
                                    <td><i class="fa fa-minus-square js-minus user-icon"></i> <input type="text" data-price="<?php echo $item['money'];?>" style="width: 20px;margin: 0 3px;" name="gnums[]" class="input rounded js-item-good-input" value="0"><i class="fa fa-plus-square js-plus user-icon"></i> </td>
                                </tr>
                            <?php endforeach;?>
                        <?php endif;?>
                        </tbody>
                    </table>

                </div>

                </div>



          

            <!-- 酒水管理-->
            <div id="drinks_config" class="js-config-warp" style="display: none" >

                <div class="h_a">
                    酒水管理
                </div>
                <div class="table_full">
                    <table class="table_list" width="100%">

                        <col width="200px"/>
                        <col width="200px"/>
                        <col width="200px"/>
                        <col width="200px"/>
                        <col width="200px"/>
                        <col/>
                        <tbody class="goods_body">
                        <tr>

                            <td>ID</td>
                            <td>酒水名</td>
                            <td>价格</td>
                            <td>备注</td>
                            <td>已选数量</td>
                        </tr>
                        <?php if($drinks_list):?>
                            <?php foreach ($drinks_list as $key=>$item):?>
                                <tr>
                                    <input type="hidden" name="gids[]" value="<?php echo $item['id'];?>">
                                    <td><?php echo $item['id'];?></td>
                                    <td><?php echo $item['name'];?></td>
                                    <td><?php echo price_format($item['money']);?></td>
                                    <td><?php echo $item['note'];?></td>
                                    <td><i class="fa fa-minus-square js-minus user-icon"></i> <input type="text" data-price="<?php echo $item['money'];?>" style="width: 20px;margin: 0 3px;" name="gnums[]" class="input rounded js-item-good-input" value="0"><i class="fa fa-plus-square js-plus user-icon"></i> </td>
                                </tr>
                            <?php endforeach;?>
                        <?php endif;?>
                        </tbody>
                    </table>

                </div>

            </div>

        
            <div class="h_a">价格信息</div>
            <div class="table_full">
                <table style="text-align: right" width="100%">
                    <tbody>


                    <tr>
                        <td>
                            房间金额：￥
                            <span class="js-span-room-account">0</span>元
                            <input name="room_account" id="js-input-room-account"  type="hidden"  value="0">
                        </td>
                    </tr>


                    <tr>
                        <td>
                            +酒水和菜品金额：￥<span id="goods_price">0.00</span>元
                        </td>
                    </tr>





                    <tr>
                        <td>= 订单总金额：￥<span id="order_price"><input type="number" class="input rounded w100" name="order_account" id="input-total-order-account" validate="{required:true,number:true}" value="0.00" /></span>元</td>
                    </tr>

                    </tbody>
                </table>
            </div>
            <div class="btn_wrap">
                <div class="btn_wrap_pd">
                    <button type="submit" class="btn btn_submit J_ajax_submit_btn">提交订单</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript">
    var urlD = '<?php echo site_url('order/adminhotel/add');?>';
    var days = '<?php echo $days?>';
    var uprice = '<?php echo $book_unit_price?>';
    var tog = false;
    var totalMarketNumbers = 6 ;
    $(function(){
       $('.js-input-room-check').click(function () {

            var $input = $(this).find('input');
            if($input.is(':disabled'))
            {
                $.dialog.tips('已入住的房间不可选');
                return false;
            }

        })

        $('.js-input-room-check').find('td:eq(0)').click(function () {
            var $input = $(this).find('input');
            $input.trigger('click');

        })

        $('.js-input-room-check').find('input').click(function () {

             $(this).trigger('click');
        })

        $.extend({check_rooom_ini:function () {

            var check = false;
            $('#js-input-check-box').find('input[type="checkbox"]').each(function () {

                if($(this).is(':checked')&&!$(this).is(':disabled')){
                    check = true;
                    return false ;
                }
            })

            if(!check) {
                $.dialog.tips('未选择任何房间');

                $('#js-config-menu').find('li:eq(1)').trigger('click');
                $("html,body").animate({scrollTop:$("#js-input-check-box").offset().top-100},500);
                return false;  // 未选择房间
            }
            return true; // 已选择房间
        }})


        $('#js-config-menu').find('li').click(function () {

            $(this).addClass('current').siblings('li').removeClass('current');
            $('.js-config-warp').hide();
            var selector = $(this).data('type')+'ig';
            $('#'+selector).show();
        })


        $('#js-user-id').select2({placeholder: "请选择微信用户",allowClear: true});

        $('.btn_submit').click(function () {



            if(!$.check_rooom_ini()) return false;

            if($.calculate_in_room_numbers() <  totalMarketNumbers)
            {
                var remainNumber = Number(totalMarketNumbers) - Number($.calculate_in_room_numbers());
                $.dialog.tips('还有'+remainNumber+'人未安排房间 !');
                return false;
            }

            $('#js-config-menu').find('li:eq(0)').trigger('click');
        })


        $.extend({
            calculate_account:function(){


                // 订单总人数
                var numbers = Number($('#male_number').val()) + Number($('#woman_number').val()) + Number($('#child_number').val())
                $('.js-numbers').empty().text(numbers);



                var room_account = 0 ; // 房间价格
                $('#js-input-check-box').find('input[type="checkbox"]').each(function () {
                    if(!$(this).is(':disabled') && $(this).is(':checked'))
                    {
                        var $input_text = $(this).closest('.js-input-room-check').find('input[type="text"]');
                        if(!$input_text.is(':disabled'))
                        {
                            room_account += Number($input_text.val()) * Number($input_text.data('price')) * days ;
                        }
                    }
                })

                $('#js-input-room-account').val(room_account);
                $('.js-span-room-account').text(room_account);



                // 商品总价
                var goods_account = 0 ;
                $('.goods_body').find('.js-item-good-input').each(function () {
                     goods_account   += Number($(this).val())*Number($(this).data('price'));
                })
                $('#goods_price').text(goods_account);

                //订单总金额
                $('#input-total-order-account').val(Number(goods_account) + Number(room_account));

                $('.js-span-child').text('儿童数 : '+Number($('#child_number').val())+'人');
                $('.js-span-male').text('成人男数 : '+Number($('#male_number').val())+'人');
                $('.js-span-woman').text('成人女数 : '+Number($('#woman_number').val())+'人');
            }
        })

        // 计算订单人数

        $.extend({
            calculate_numbers:function(){
                var numbers = Number($('#male_number').val()) + Number($('#woman_number').val()) + Number($('#child_number').val())
                $('.js-numbers').empty().text(numbers);
                totalMarketNumbers  = numbers ;
                return numbers ;
            }
        })



        // 计算房间输入入住人数

        $.extend({
            calculate_in_room_numbers:function(){
                var total = 0;
                $('#js-input-check-box').find('input[type="text"]').each(function () {

                    var $input_check = $(this).closest('.js-input-room-check').find('input[type="checkbox"]');
                     if(!$input_check.is(':disabled') && $input_check.is(':checked'))
                    {
                        total += Number($(this).val());
                    }
                })

                return total ;

            }
        })


        $.calculate_account();
        
        
        $('.js-input-numbers').change(function () {

             if($.calculate_numbers() == 0){

                $(this).val(1);

             }
            $.calculate_account();
        })



        $('#js-user-id').change(function () {

            var uid = $(this).val();
            var name = $(this).find('option:selected').text();

            $('#js-contact-name').val('');
            $('#js-contact-phone-num').val('');

            if(uid !== '0') {

                $.post('<?php echo site_url('member/admin/ajax_memeber')?>',{
                    id:uid
                },function(data){
                    if(data.status=='1'){

                        var json = eval("("+data.info+")");
                        $('#js-contact-name').val(json['realname']);
                        $('#js-contact-phone-num').val(json['mobile']);
                    }
                },'json')

                $('#js-wx-nick').val(name);
            }
        })

      /*  $('#input-order-account').change(function () {


            $('#room_price').text($('#input-order-account').val());
            $.calculate_account();
        })
*/



        // 减少房间人数
        $('.js-market-minus').click(function () {


            // 提示先选择房间
            var $input_check = $(this).closest('.js-input-room-check').find('input[type="checkbox"]');


            var $input = $(this).siblings('input');

            if($input.is(':disabled')) return false;

            var ival = $input.val();

            if(ival !== '0'){

                ival -= 1;
                $input.val(ival);
            }



        })

        // 增加房间人数

        $('.js-market-plus').click(function () {


            // 提示先选择房间
            var $input_check = $(this).closest('.js-input-room-check').find('input[type="checkbox"]');
            if(!$input_check.is(':disabled') && !$input_check.is(':checked'))
            {
                $.dialog.tips('先选择房间');
                return false;
            }


            // 计算已经选择的人数
            var inRoomNumbers = $.calculate_in_room_numbers();
            if(inRoomNumbers > (totalMarketNumbers -1))
            {
                $.dialog.tips('操作已入住人数不能大于订单人数');
                return false;
            }


            var $input = $(this).siblings('input');

            if($input.is(':disabled'))  {

                $.dialog.tips('已入住房间不可选');
                return false;
            }

            var ival = parseInt($input.val());

            ival += 1;
            $input.val(ival);


            // 计算订单价格
            $.calculate_account();
        })


        $('.js-minus').click(function () {

            var $input = $(this).siblings('input');

            var ival = $input.val();

            if(ival !== '0'){

                ival -= 1;
                $input.val(ival);
            }
            $.calculate_account();

        })

        $('.js-plus').click(function () {

            var $input = $(this).siblings('input');

            var ival = parseInt($input.val());

            ival += 1;
            $input.val(ival);
            $.calculate_account();
        })

        new Kalendae.Input('start_date', {
            months:1,
            mode:'single',
            format:'YYYY-MM-DD',
            direction:'today-future'
        });


        new Kalendae.Input('end_date', {
            months:1,
            mode:'single',
            format:'YYYY-MM-DD',
            direction:'future'
        });



        $('#start_date').change(function () {


            var date = $(this).val();
            var month = '';
            var day = '';
            d = new Date(date);
            d = +d + 1000*60*60*24;
            d = new Date(d);



            if((d.getMonth()+1).toString().length == 2){
                month = (d.getMonth()+1);
            }else {
                month = '0'+(d.getMonth()+1);
            }

            if(d.getDate().toString().length == 2){
                day = d.getDate();
            }else {
                day = '0'+(d.getDate());
            }

            var newdate = d.getFullYear()+"-"+month+"-"+day;




            $('#end_date').val(newdate);



            var end = $('#end_date').val();
            var df_date = $('#end_date').data('default');

            if(end !== df_date)
            {
                var url = urlD+'?start='+date+'&end='+end ;
                $.dialog.tips('加载中...');
                window.location.href = url;
            }


        })

        $('#end_date').change(function () {

            var start = $('#start_date').val();
            var end = $('#end_date').val();
            var url = urlD+'?start='+start+'&end='+end ;
            $.dialog.tips('加载中...');
            window.location.href = url;

        })




    })
</script>
</body>
</html>